﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using BLL.Artwork;
using BLL.ArtworkLike;

namespace WebApplication.Art
{
    public partial class Handle : BaseHandle
    {
        private ArtworkService artworkService = new ArtworkService();
        private ArtworkLikeService artworkLikeService = new ArtworkLikeService();

        public void Like()
        {
            int uid = Convert.ToInt32(Session["UID"]);
            int artworkId = Convert.ToInt32(Request.Form["id"]);

            // check user liked status.
            bool liked = artworkLikeService.ExistLikeArtwork(artworkId, uid);

            if (!liked)
            {
                artworkLikeService.LikeArtwork(new Models.ArtworkLikes() { ArtworkID = artworkId, OwnerID = uid });

                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "点赞成功" }));
            }
            else
            {
                Models.ArtworkLikes artworkLike = artworkLikeService.GetArtworkLike(artworkId, uid);
                artworkLikeService.UnLikeArtwork(artworkLike);
                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "取消点赞成功" }));
            }
        }

        public void Insert()
        {
            Utils.Model<Models.Artworks> modelUtils = new Utils.Model<Models.Artworks>();
            Models.Artworks artwork = modelUtils.GetModel(Request.Form);
            artwork.OwnerID = Convert.ToInt16(Session["UID"]);

            artworkService.SaveArtwork(artwork);

            if (artwork.ArtworkID != null)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, data = new { id = artwork.ArtworkID }, msg = "作品发布成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "作品发布失败" }));
            }
        }

        public void Update()
        {
            Utils.Model<Models.Artworks> modelUtils = new Utils.Model<Models.Artworks>();
            Models.Artworks artwork = modelUtils.GetModel(Request.Form);

            Models.Artworks queryArtwork = artworkService.GetArtworkById(artwork.ArtworkID);
            int hot = queryArtwork.Hot;
            int uid = Convert.ToInt32(Session["UID"]);
            Models.User user = ((Models.User)Session["USER"]);

            if (queryArtwork.OwnerID != uid)
            {
                if(user.Role != 2)
                {
                    Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "您无权更新该作品!" }));
                    return;
                }
            }

            artwork.OwnerID = uid;
            artwork.Hot = hot;

            if (artworkService.UpdateArtwork(artwork))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, data = new { id = artwork.ArtworkID }, msg = "作品更新成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "作品更新失败" }));
            }
        }

        public void Delete()
        {

            int id = Convert.ToInt32(Request.Form["id"]);
            Models.Artworks queryArtwork = artworkService.GetArtworkById(id);
            int uid = Convert.ToInt32(Session["UID"]);

            if (queryArtwork.OwnerID != uid)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "您无权删除该作品!" }));
                return;
            }


            if (artworkService.DeleteArtworkById(id))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "作品删除成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "作品删除失败" }));
            }
        }

        private class SortForm
        {
            public int id { get; set; }
            public int orderNum { get; set; }
        }

        public void Sort()
        {

           try
            {
                List<SortForm> data = JsonConvert.DeserializeObject<List<SortForm>>(Request.Form["data"]);

                foreach (SortForm sort in data)
                {
                    Models.Artworks artwork = artworkService.GetArtworkById(sort.id);
                    artwork.OrderNum = sort.orderNum;
                    artworkService.UpdateArtwork(artwork);
                }

                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "作品顺序更新成功" }));
            } catch
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "作品顺序更新失败" }));
            }
           


        }

    }
}